<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>后台首页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
    <link rel="stylesheet" href="${ctxPath}/static/manage/layui/css/layui.css" media="all">
    <script src="${ctxPath}/static/manage/layui/layui.js"></script>
    <script src="${ctxPath}/static/manage/js/jquery.js"></script>
</head>
<body>
<div class="x-nav">
        <span class="nav-breadcrumb">
            <a><cite>后台管理></cite></a>
            <a><cite>药品管理></cite></a>
            <a><cite>人员管理</cite></a>
        </span>
</div>
<div class="x-body">
    <blockquote class="layui-elem-quote quoteBox">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" class="layui-input searchContent"  placeholder="请输入文件名关键字" />
            </div>
            <a class="layui-btn search_btn" data-type="reload">搜索</a>
            <input type="button" class="layui-btn" onclick="AddDrug()" value="添加药品" />
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn">
                <a href="/manage/drug/exportDrug" class="layui-icon layui-icon-layim-download">导出</a>
            </button>
        </div>
        <div class="layui-btn" id="uploadFile">
            <i class="layui-icon">&#xe67c;</i>导入
        </div>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </blockquote>
    <table id="drugTable" lay-filter="drugTable"></table>

</div>
</body>
</html>
<script>
    layui.use(['jquery', 'upload'], function () {
        // 添加文件 上传服务器
        var upload = layui.upload;
        upload.render({
            elem: '#uploadFile',
            exts: 'doc|docx|xls|xlsx|txt|pdf|zip|rar',
            url: '/manage/drug/importDrug',
            done: function(result){
                layer.closeAll('loading');
                alert(result.data);
                window.location.reload();
            },
            error: function(result){
                layer.closeAll('loading');
                alert(result.data);
                window.location.reload();
            }
        });
    });
</script>
<!--添加医生弹出层-->
<script type="text/javascript">
    function AddDrug() {
        layui.use('layer', function() {
            var layer = layui.layer;
            layer.open({
                type : 2,
                area : [ '800px', '600px' ],
                content : '/manage/drug/toAdd'
            });
        });
    }

</script>
<!--<script type="text/html" id="titleTpl">
    <img id='{{d.id}}' src='img/{{d.url}}' />

</script>-->
<script type="text/javascript">
    layui.use(['jquery', 'upload', 'table', 'layer', 'util'], function(){
        var $ = layui.$, table = layui.table, layer = layui.layer, upload = layui.upload, util = layui.util;
        var active = {
            reload: function(){
                table.reload('drugtable_id', {
                    where: {
                        name: $('.searchContent').val(),
                        page: 1
                    }
                });
            }
        };
        /*static/page/images/8.jpg*/
        table.render({
            elem: '#drugTable'
            ,id: 'drugtable_id'
            ,height: 500
            ,url: '/manage/drug/findAll'
            ,page: true
            ,limit: 8
            ,limits : [5,10,15,20]
            ,cols: [[
                { checkbox: true, fixed: true },
                { field: 'id',width:80, title: '序号', sort: true },
                { field: 'name',width:100, title: '药品名称' },
                {field: 'url', width:80,title: '图标',templet:function (n) {
                    return '<div><img src='+'${ctxPath}/'+n.url+'></div>' }
                },
                { field: 'pname',width:120, title: '类型名称' },
                { field: 'effect',width:150, title: '效果' },
                { field: 'buytime',width:150, title: '上架时间'},
                { field: 'updatetime',width:150, title: '修改时间'},
                { field: 'price',width:100, title: '价格' },
                { field: 'tempId', title: '操作', width:'28%', templet: function(d){
                        return '<a class="layui-btn layui-btn-xs" lay-event="edit" lay-id="'+ d.tempId +'"><i class="layui-icon layui-icon-edit"></i>编辑</a>' +
                            '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" lay-id="'+ d.tempId +'"><i class="layui-icon layui-icon-del"></i>移除</a>'+
                            '<a class="layui-btn layui-btn-xs layui-btn-read" lay-event="read" lay-id="'+ d.tempId +'"><i class="layui-icon layui-icon-read"></i>查看</a>';
                    }
                }
            ]]
        });
// 搜索（利用定义的active重新载入表格）
        $('.search_btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        // 监听工具条
        table.on('tool(drugTable)', function (obj) {
            // data当前行数据， lay-event或event 对应的值，当前行tr的DOM对象
            var data = obj.data, event = obj.event, tr = obj.tr;
            console.log(data);
            switch(event){
                case "read":
                    DrugRead(data);
                    break;
                case "del":
                    layer.confirm('真的删除行么？', function(index){
                        layer.close(index);
                        $.ajax({
                            type: 'post',
                            url: '/manage/drug/del/'+data.id,
                           /* data: {'id': data.id},*/
                            success:function(result){
                                    alert(result);
                                window.location.reload();

                            }
                        });
                    });
                    break;
                case "edit":
                    DrugUpdate(data);
                    break;
                case "state":
                    //var stateValue = tr.find("input[name='state']").prop('checked') ? 1 : 2;
                    $.ajax({
                        type: 'post',
                        url: '/manage/drug/update',
                        data: {'id': data.id, 'stateValue': stateValue, 'type': data.type},
                        success:function(result){
                            alert(result);
                            location.replace(location.href);                // 刷新
                        }
                    });
                    break;
            }
        });
        // 查看
        function DrugRead(rowData) {
            layer.open({
                title : "查看" + rowData.name,
                type : 2,
                fix: false, //不固定
                maxmin: true,
                area: ['1000px', '600px'],
                content: "/manage/drug/read/" + rowData.id
            });
        }
        /*// 删除
        function  DrugDel(rowData) {
            layer.confirm('真的删除行么？', function(index){
                layer.close(index);
                $.ajax({
                    type: 'post',
                    url: '/manage/drug/del',
                    data: {'id': data.id},
                    success:function(result){
                        alert(result);
                        window.location.reload();      // 刷新
                    }
                });
            });
        }*/

        // 编辑
        function DrugUpdate(rowData) {
            layer.open({
                title : "编辑" + rowData.name,
                type : 2,
                fix: false, //不固定
                maxmin: true,
                area: ['1000px', '600px'],
                content: "/manage/drug/toUpdate/" + rowData.id,
              /*  success: function (layero, index) {
                    // 回显下拉框
                    var body = layui.layer.getChildFrame('body', index);
                    body.find(".typeSelect select").val(articleType);
                }*/
            });
        }


    });

</script>
